package com.atguigu2.dao;

import com.beans.Customers;

import java.sql.Connection;
import java.sql.Date;
import java.util.List;

/**
 * @auther user
 * @date 2021/12/10 - 1:29
 */
public class CustomersDaoImpl extends BaseDao implements CustomersDao {
    @Override
    public void inert(Connection connection, Customers customers) {
        String sql = "insert into customers(name,email,birth) values(?,?,?)";
        update(connection, sql, customers.getName(), customers.getEmail(), customers.getBirth());
    }

    @Override
    public void deleteById(Connection connection, int id) {
        String sql = "delete from customers where id = ?";
        update(connection,sql,id);
    }

    @Override
    public void updateById(Connection connection, Customers customers) {
        String sql = "update customers set name = ?,email = ?,birth = ? where id = ?";
        update(connection,sql,customers.getName(),customers.getEmail(),customers.getBirth(),customers.getId());
    }

    @Override
    public Customers getCustomersById(Connection connection, int id) {
        String sql = "select id,name,email,birth from customers where id = ?";
        return  getInstance(connection,Customers.class,sql,id);
    }

    @Override
    public List<Customers> getAll(Connection connection) {
        String sql = "select id,name,email,birth from customers";
        return getForList(connection,Customers.class,sql);
    }

    @Override
    public Long getCount(Connection connection) {
        String sql = "select count(*) from customers";
        return getValue(connection,sql);
    }

    @Override
    public Date getMaxBirth(Connection connection) {
        String sql = "select max(birth) from customers";
        return getValue(connection,sql);
    }
}
